let BaseUrl
//开发环境中
if (process.env.NODE_ENV === 'development') {
    // 开发环境 http://txyf.wnb56.com/api
    // BaseUrl = 'http://172.16.3.135:8080'
    // BaseUrl = 'http://txyf.wuniubang.com/api'  //开发环境请求地址
    BaseUrl = 'https://ht.cqtxyf.com/api'  //开发环境请求地址
} else {
    // 生产环境
    BaseUrl = 'https://ht.cqtxyf.com/api'  //生成环境请求地址
}

export const request = (options) => {
    return new Promise((resolve, reject) => {
        const {needToken} = options;
        // 获取用户令牌
        let token = uni.getStorageSync('token')

        // 设置请求头
        const header = {
            'Content-Type': 'application/json',
            Authorization: token,
            ...options.header, // 可以传入额外的请求头参数
        }
        console.log(options.data)
        uni.showLoading({
            mask: true,
            duration: 5000
        });
        // ⭐在发送请求之前执行拦截操作
        uni.request({
            url: BaseUrl + options.url, //接收请求的API
            method: options.method || 'GET', //接收请求的方式,如果不传默认为GET
            data: options.data || {}, //接收请求的data,不传默认为空
            header: (needToken == undefined || needToken) ? header : {}, //接收请求的header
            // header: header, //接收请求的header
            success(res) {
                setTimeout(() => uni.hideLoading(), 1000)
                const {data = {}} = res;
                // ⭐在这里处理接收到响应后处理响应数据
                if (['B0602', 'A0307', 'A1871'].includes(data.code)) {
                    // 提示重新登录
                    // uni.showToast({
                    //     title: '登录过期，请重新登录',
                    //     icon: 'none'
                    // })
                    // useUserStore().userLogout()
                    setTimeout(() => {
                        uni.reLaunch({
                            url: '/pages/login/login'
                        })
                    }, 1000)
                    return;
                } else if (!data.success && !['U10001'].includes(data.code)) {
                    return uni.showToast({icon: "error", title: data.message})
                }
                resolve(res.data) // 使用resolve将数据传递出去
            },
            fail: (err) => {
                setTimeout(() => uni.hideLoading(), 1000)
                reject(err)
            }
        })
    })
}

export class UploadRequest {
    static convertFileIds( fileIds, fileId) {
        return [ ...fileIds.split(","), fileId].filter(Boolean)
            .join(",")
    }
    static upload(filepath, fileIds = "") {
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                url: BaseUrl + '/sysFileInfo/upload', //接收请求的API
                name: 'file',
                filePath: filepath,
                formData: { 'secretFlag': 'Y' },
                header: {
                    'Content-Type': 'application/json',
                    Authorization: uni.getStorageSync('token')
                },
                success: (res) => {
                    const { data = {}} = JSON.parse(res.data);
                    resolve(data)
                },
                fail: (err) => {
                    reject(err)
                }
            })
        })
    }

    static getFileInfo( fileIds) {
        return request({
            url: '/fileInfo/getFileAuthPath?fileIds=' + fileIds,
            method: 'GET'
        })
    }
}
